home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / viewwrld / viewwrld.lha / viewworld / gr / confirmer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-21  |  7.7 KB  |  261 lines

  1. /* $Id: confirmer.c,v 2.3 89/09/20 17:01:24 mbp Exp $
  2.  *
  3.  * confirmer.c: confirmer procedures
  4.  */
  5.  
  6. /************************************************************************
  7.  *        Copyright (C) 1989 by Mark B. Phillips                  *
  8.  *                                     *
  9.  * Permission to use, copy, modify, and distribute this software and    *
  10.  * its documentation for any purpose and without fee is hereby granted, *
  11.  * provided that the above copyright notice appear in all copies and    *
  12.  * that both that copyright notice and this permission notice appear in *
  13.  * supporting documentation, and that the name of Mark B. Phillips or   *
  14.  * the University of Maryland not be used in advertising or publicity   *
  15.  * pertaining to distribution of the software without specific, written *
  16.  * prior permission.  This software is provided "as is" without express *
  17.  * or implied warranty.                                                 *
  18.  ************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include "gr.h"
  22. #include "internal.h"
  23.  
  24. #define STANDARD_MESSAGE_MAX_LENGTH 29
  25. #define STANDARD_PANEL_WIDTH 290
  26. #define STANDARD_PANEL_HEIGHT 77
  27. #define STANDARD_MESSAGE_X 44
  28. #define STANDARD_MESSAGE_Y 16
  29.  
  30. Frame            GR_confirmer_frame;
  31. Panel            GR_confirmer_panel;
  32. static Panel_item    confirmer_message;
  33. static Panel_item    yes_image, yes_word, no_image, no_word;
  34.  
  35. static Cursor confirmer_cursor;
  36.  
  37. static void confirmer_event_proc();
  38.  
  39. static short confirmer_array[] = {
  40. #include "images/confirmer.image"
  41. };
  42. mpr_static(confirmer_pr, 16, 16, 1, confirmer_array);
  43.  
  44. /*-----------------------------------------------------------------------
  45.  * Function:    GR_initialize_confirmer
  46.  * Description:    initialize the confirmer window
  47.  * Args:    (none)
  48.  * Notes:    Does not display anything - just sets things up
  49.  */
  50. int
  51. GR_initialize_confirmer()
  52. {
  53.   confirmer_cursor =
  54.     cursor_create(CURSOR_IMAGE, &confirmer_pr, 0);
  55.  
  56.   GR_confirmer_frame =
  57.     window_create(GR_base_frame, FRAME,
  58.           FRAME_SHOW_LABEL, FALSE,
  59.           WIN_SHOW, FALSE,
  60.           WIN_CURSOR, confirmer_cursor,
  61.           0);
  62.   if (GR_confirmer_frame == NULL) return(1);
  63.   
  64.   GR_confirmer_panel =
  65.     window_create(GR_confirmer_frame, PANEL,
  66.           WIN_X, 0,
  67.           WIN_Y, 0,
  68.           WIN_WIDTH, STANDARD_PANEL_WIDTH,
  69.           WIN_HEIGHT, STANDARD_PANEL_HEIGHT,
  70.           WIN_FONT, GR_bold_font,
  71.           PANEL_BACKGROUND_PROC, confirmer_event_proc,
  72.           PANEL_EVENT_PROC, confirmer_event_proc,
  73.           WIN_CURSOR, confirmer_cursor,
  74.           0);
  75.   if (GR_confirmer_panel == NULL) return(1);
  76.   
  77.   confirmer_message =
  78.     panel_create_item(GR_confirmer_panel, PANEL_MESSAGE, 
  79.               PANEL_ITEM_X, STANDARD_MESSAGE_X,
  80.               PANEL_ITEM_Y, STANDARD_MESSAGE_Y,
  81.               PANEL_LABEL_STRING, "",
  82.               0);
  83.   
  84.   yes_image =
  85.     panel_create_item(GR_confirmer_panel, PANEL_MESSAGE, 
  86.               PANEL_ITEM_X, 62,
  87.               PANEL_ITEM_Y, 49,
  88.               PANEL_LABEL_IMAGE, GR_mouse_image_pr_ptr(0),
  89.               0);
  90.  
  91.   yes_word =
  92.     panel_create_item(GR_confirmer_panel, PANEL_MESSAGE, 
  93.               PANEL_ITEM_X, 80,
  94.               PANEL_ITEM_Y, 49,
  95.               PANEL_LABEL_STRING, "Yes",
  96.               0);
  97.   
  98.   no_image =
  99.     panel_create_item(GR_confirmer_panel, PANEL_MESSAGE, 
  100.               PANEL_ITEM_X, 192,
  101.               PANEL_ITEM_Y, 49,
  102.               PANEL_LABEL_IMAGE, GR_mouse_image_pr_ptr(2),
  103.               0);
  104.  
  105.   no_word =
  106.     panel_create_item(GR_confirmer_panel, PANEL_MESSAGE, 
  107.               PANEL_ITEM_X, 210,
  108.               PANEL_ITEM_Y, 49,
  109.               PANEL_LABEL_STRING, "No",
  110.               0);
  111.  
  112.   standardize_confirmer();
  113.   return(0);
  114. }
  115.  
  116. /*-----------------------------------------------------------------------
  117.  * Function:    GR_user_confirm
  118.  * Description:    display message in confirmer panel and get response
  119.  *         from user
  120.  * Args  IN:    s: message to be displayed
  121.  * Returns:    1 for YES, 0 for NO
  122.  * Notes:    This procedure blocks.
  123.  *
  124.  *    original version is is confirm.orig during testing of this version
  125.  */
  126. int
  127. GR_user_confirm(s)
  128. char *s;
  129. {
  130.   int answer, msglen, width;
  131.   struct pr_subregion bound;
  132.   Cursor cursor;
  133.  
  134.   /* Write message centered in confirmer frame, and resize if necessary */
  135.   msglen = strlen(s);
  136.   pf_textbound(&bound, msglen, GR_bold_font, s);
  137.   if (msglen > STANDARD_MESSAGE_MAX_LENGTH) {
  138.     /* message is too long for standard size, so resize the panel */
  139.     width = bound.size.x + 20;
  140.     window_set(GR_confirmer_panel,
  141.            WIN_WIDTH, width,
  142.            WIN_HEIGHT, STANDARD_PANEL_HEIGHT,
  143.            0);
  144.     panel_set(confirmer_message,
  145.           PANEL_ITEM_X, 10,
  146.           PANEL_LABEL_STRING, s,
  147.           0);
  148.     set_yes_no_position();
  149.     window_fit(GR_confirmer_frame);
  150.   }
  151.   else {
  152.     /* message will fit in standard confirmer size; so center it */
  153.     panel_set(confirmer_message,
  154.           PANEL_ITEM_X, (STANDARD_PANEL_WIDTH-bound.size.x)/2,
  155.           PANEL_LABEL_STRING, s,
  156.           0);
  157.   }
  158.   center_window(GR_confirmer_frame, 0, 0, GR_base_frame);
  159.  
  160.   /* Beep and get user's answer */
  161.   GR_hold_error_message(1);
  162.   window_bell(GR_base_frame);
  163.   answer = (int)window_loop(GR_confirmer_frame);
  164.  
  165.   /* Return confirmer window to standard size, in case it was resized */
  166.   standardize_confirmer();
  167.  
  168.   return(answer);
  169. }
  170.  
  171. /*-----------------------------------------------------------------------
  172.  * Function:    confirmer_event_proc
  173.  * Description:    process an event which happened while waiting for user's
  174.  *          answer
  175.  * Args  IN:    item: the item which received the event
  176.  *        *event: the event
  177.  */
  178. static void
  179. confirmer_event_proc( item, event )
  180. Panel_item item;
  181. Event *event;
  182. {
  183.   switch (event_id(event)) {
  184.   case MS_LEFT:
  185.     window_return(1);
  186.     break;
  187.   case MS_RIGHT:
  188.     window_return(0);
  189.     break;
  190.   default:
  191.     break;
  192.   }
  193. }
  194.  
  195. /*-----------------------------------------------------------------------
  196.  * Function:    center_window
  197.  * Description:    center one window wrt another
  198.  * Args  IN:    inner_window: handle of "inner" window
  199.  *        x_offset, y_offset: added to x,y coords of centered window
  200.  *        outer_window: handle of "outer" window
  201.  * Notes:    This calls window_set to reset the WIN_X,WIN_Y attributes
  202.  *        of the inner window.
  203.  */
  204. static int
  205. center_window(inner_window, x_offset, y_offset, outer_window )
  206. Window inner_window, outer_window;
  207. int x_offset, y_offset;
  208. {
  209.   int inner_w, inner_h, outer_w, outer_h;
  210.   int inner_x, inner_y;
  211.  
  212.   inner_w = (int) window_get( inner_window, WIN_WIDTH );
  213.   inner_h = (int) window_get( inner_window, WIN_HEIGHT );
  214.   outer_w = (int) window_get( outer_window, WIN_WIDTH );
  215.   outer_h = (int) window_get( outer_window, WIN_HEIGHT );
  216.   inner_x = x_offset + (outer_w - inner_w) / 2;
  217.   inner_y = y_offset + (outer_h - inner_h) / 2;
  218.   window_set( inner_window, WIN_X, inner_x, WIN_Y, inner_y, 0 );
  219. }
  220.  
  221. /*-----------------------------------------------------------------------
  222.  * Function:    set_yes_no_position
  223.  * Description:    Set the horizontal positions of the "yes" and "no"
  224.  *          images in the confirmer panel
  225.  * Args:    (none)
  226.  * Notes:    These are set to about 1/3 (for "yes") and 2/3 (for "no")
  227.  *        of the width of the panel.
  228.  */
  229. static int
  230. set_yes_no_position()
  231. {
  232.   int width, yes_x, no_x;
  233.  
  234.   width = (int)window_get(GR_confirmer_panel, WIN_WIDTH);
  235.   yes_x = (width-60)/3;
  236.   no_x = 2*yes_x + 30;
  237.  
  238.   panel_set(yes_image, PANEL_LABEL_X, yes_x, 0);
  239.   panel_set(yes_word, PANEL_LABEL_X, yes_x+26, 0);
  240.   panel_set(no_image, PANEL_LABEL_X, no_x, 0);
  241.   panel_set(no_word, PANEL_LABEL_X, no_x+26, 0);
  242. }
  243.  
  244. /*-----------------------------------------------------------------------
  245.  * Function:    standardize_confirmer
  246.  * Description:    reset the sizes of the confirmer panel and window
  247.  *          to their initial ("standard") size.
  248.  * Args:    (none)
  249.  */
  250. static int
  251. standardize_confirmer()
  252. {
  253.   window_set(GR_confirmer_panel, WIN_WIDTH, STANDARD_PANEL_WIDTH, 0);
  254.   window_set(GR_confirmer_panel, WIN_HEIGHT, STANDARD_PANEL_HEIGHT, 0);
  255.   panel_set(confirmer_message, PANEL_ITEM_X, STANDARD_MESSAGE_X, 0);
  256.   panel_set(confirmer_message, PANEL_ITEM_Y, STANDARD_MESSAGE_Y, 0);
  257.   set_yes_no_position();
  258.   window_fit(GR_confirmer_frame);
  259. }
  260.  
  261.